package com.zlzxm.studentscreen.net.websocket;

import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

public class JWebSocketClient extends WebSocketClient {


    private JWebSocketListener jWebSocketListener;

    public void setjWebSocketListener(JWebSocketListener jWebSocketListener) {
        this.jWebSocketListener = jWebSocketListener;

    }

    public JWebSocketClient(URI serverUri) {
        super(serverUri, new Draft_6455());
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        Log.e("JWebSocketClient", "onOpen()");
        if(jWebSocketListener!=null) {
            jWebSocketListener.onOpen(handshakedata);
        }
    }


    @Override
    public void onMessage(String message) {
        Log.e("JWebSocketClient", "onMessage():"+message);

        if(jWebSocketListener!=null) {

            jWebSocketListener.onMessage(message);
        }
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        Log.e("JWebSocketClient", "onClose()  code:"+code + "reason:"+reason + "isremote:"+remote);
        if(jWebSocketListener != null) {

            jWebSocketListener.onClose(code,reason,remote);

        }
    }

    @Override
    public void onError(Exception ex) {
        Log.e("JWebSocketClient", "onError()");
        if(jWebSocketListener!=null) {
            jWebSocketListener.onError(ex);
        }
    }

}